/**
 * 单一资源通过版本号加载管理类
 */
class ResVersionManager {
    public static resVersionData: any;
    private complateFunc: Function;
    private complateFuncTarget: any;

    /**
     * 构造函数
     */
    public constructor() {
    }

    public init(): void {
        // this.initVersion();
    }

    /**
     * Res加载使用版本号的形式
     */
    private initVersion(): void {
        RES.getVirtualUrl = function (url) {
            var version: string = "";
            var resVersion: any = ResVersionManager.resVersionData;
            var urlTemp: string = url; //url.substring(9);
            if (resVersion && resVersion[urlTemp]) {
                version = resVersion[urlTemp];
            }

            if (version.length == 0) {
                version = Math.random() + "";
            }

            if (url.indexOf("?") == -1) {
                url += "?v=" + version;
            } else {
                url += "&v=" + version;
            }
            return url;
        }
    }

    /**
     * 加载资源版本号配置文件
     * @param url 配置文件路径
     * @param complateFunc 加载完成执行函数
     * @param complateFuncTarget 加载完成执行函数所属对象
     */
    public loadConfig(url: string, complateFunc: Function, complateFuncTarget: any): void {
        this.complateFunc = complateFunc;
        this.complateFuncTarget = complateFuncTarget;
        RES.getResByUrl(url, this.loadResVersionComplate, this);
    }

    /**
     * 配置文件加载完成
     * @param data
     */
    private loadResVersionComplate(data: any): void {
        ResVersionManager.resVersionData = data;
        this.complateFunc.call(this.complateFuncTarget);
    }

    private static _instance: ResVersionManager;
    public static it(): ResVersionManager {
        if (!this._instance)
            this._instance = new ResVersionManager();

        return this._instance;
    }
}
